package com.financing.wap.web.controller.api;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dto.DstMemberInfoDTO;
import com.financing.wap.dto.DstUserWithdrawLogDTO;
import com.financing.wap.service.IDstMemberInfoService;
import com.financing.wap.service.IDstUserWithdrawLogService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("dstUserWithdrawLogController")
@RequestMapping("/api/dstUserWithdrawLog")
public class DstUserWithdrawLogController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserWithdrawLogService dstUserWithdrawLogService;
	
	@Autowired
	private IDstMemberInfoService dstMemberInfoService;
	
	@RequestMapping("/listDstUserWithdrawLog")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstUserWithdrawLogDTO dstUserWithdrawLog,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstUserWithdrawLog list begin .......");
			
			dstUserWithdrawLog.setBegin(pager.getStartIndex());
			dstUserWithdrawLog.setPageSize(pager.getPageSize());
			
			int count = dstUserWithdrawLogService.queryListCount(dstUserWithdrawLog);
			
			pager.setTotalCount(count);
			
			List<DstUserWithdrawLogDTO> dstUserWithdrawLogList = null;
			
			if(count > 0){
				dstUserWithdrawLogList = dstUserWithdrawLogService.queryList(dstUserWithdrawLog);
			}else{
				dstUserWithdrawLogList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstUserWithdrawLogList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstUserWithdrawLog list end .......");
		}catch(Exception e){
			log.error("query dstUserWithdrawLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 提现
	 * @param request
	 * @param dstUserWithdrawLog
	 * @return
	 */
	@RequestMapping("/userWithdraw")
	@ResponseBody
	public synchronized ResultMap userWithdraw(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstUserWithdrawLog  begin .......");
			
			String openid = request.getParameter("openid");
			if(StringUtil.isNullOrEmptyStr(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}			
			DstMemberInfoDTO dstMemberInfo=new DstMemberInfoDTO();
			dstMemberInfo.setUserOpenid(openid);
			dstMemberInfo.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			DstMemberInfoDTO member = dstMemberInfoService.queryListGetFirst(dstMemberInfo);
			BigDecimal memberMoney = member.getMemberMoney();
			//不足50不能提现
			if(memberMoney==null||memberMoney.compareTo(new BigDecimal(50))==-1){
				result.setStatusCode(ResultMap.WITHDRAW_MONEY_MIN);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			boolean withdraw;
			try {
				withdraw = dstUserWithdrawLogService.userWithdraw(openid,memberMoney);
				result.setAttribute("withdraw",withdraw);
			} catch (Exception e) {
				result.setAttribute("withdraw",false);
				logger.error("pay error",e);
			}
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstUserWithdrawLog list end .......");
		}catch(Exception e){
			log.error("add dstUserWithdrawLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
